import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { STColumn, STComponent, STPage, STChange } from '@delon/abc/table';
import { SFSchema, SFComponent, SFRadioWidgetSchema } from '@delon/form';
import { GridComponent, PopdetailComponent, HttpService, SupDic } from 'lbf';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { ReuseTabService } from '@delon/abc/reuse-tab';
import { LbWjfwComponent } from '@shared/components/lb_wjfw/lbwjfw.component';

@Component({
  selector: 'app-ybzh-ylbxlr',
  templateUrl: './ylbxlr.component.html',
})
export class YbzhYlbxlrComponent implements OnInit {
  params = { sname: 'YLBXCL_QUERY', form: {} };
  dataset = [];
  isVisible = false;
  chVisible = false;
  isPzVisible = false;
  formData = {};
  formData1 = {};
  date2: any = {};
  PROJID = '1';
  DAD069 = '';
  CKC009: any = {};
  changeSchema: SFSchema = {
    properties: {},
  };
  modalPzTitle = '中心项目编码';
  slSchema: SFSchema = {
    properties: {
      DAD095: {
        type: 'string',
        title: '',
        enum: [
          { label: '办理', value: '1' },
          { label: '不办理', value: '0' },
        ],
        // tslint:disable-next-line: no-object-literal-type-assertion
        ui: {
          widget: 'radio',
          // asyncData: () => of().pipe(delay(100)),
          change: console.log,
          grid: {
            span: 24,
          },
        } as SFRadioWidgetSchema,
        default: '1',
      },
      DAD098: {
        type: 'string',
        title: '受理建议',
        ui: {
          visibleIf: { DAD095: ['0'] },
          spanLabelFixed: 80,
          grid: { span: 24 },
        },
      },
    },
    ui: { grid: { span: 24 } },
  };

  constructor(
    private lbservice: HttpService,
    public msgSrv: NzMessageService,
    private supDic: SupDic,
    private reuseTabService: ReuseTabService,
    private activateroute: ActivatedRoute,
  ) {
    this.reuseTabService.title = '医疗报销录入';
  }

  sdSchema: SFSchema = {
    properties: {
      YKB003: {
        type: 'string',
        title: '参保地机构',
        enum: this.supDic.getSFDic('YKB003'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC801: {
        type: 'string',
        title: '就医地机构',
        enum: this.supDic.getSFDic('AKC801'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      CKC009: {
        type: 'string',
        title: '交易流水号',
        ui: { hidden: true, grid: { span: 6 } },
      },
      AAC001: {
        type: 'string',
        title: '个人编号',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAC002: {
        type: 'string',
        title: '身份证',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAC003: {
        type: 'string',
        title: '姓名',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAC004: {
        type: 'string',
        title: '性别',
        enum: this.supDic.getSFDic('AAC004'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC020: {
        type: 'string',
        title: '医疗保险卡号',
        readOnly: true,
        ui: {
          grid: { span: 6 },
        },
      },
      AKC100: {
        type: 'string',
        title: '医保人员类别',
        enum: this.supDic.getSFDic('AKC100'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKB001: {
        type: 'string',
        title: '医院编号',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKA101: {
        type: 'string',
        title: '医院等级',
        enum: this.supDic.getSFDic('AKA101'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      YYMC: {
        type: 'string',
        title: '医院名称',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKB002: {
        type: 'string',
        title: '实际医院名称',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKA002: {
        type: 'string',
        title: '医疗机构类别',
        enum: this.supDic.getSFDic('AKA002'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKA130: {
        type: 'string',
        title: '医疗类别',
        enum: this.supDic.getSFDic('AKA130'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC001: {
        type: 'string',
        title: '就诊类别',
        enum: this.supDic.getSFDic('AKC001'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC038: {
        type: 'string',
        title: '门诊住院号',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAB004: {
        type: 'string',
        title: '单位名称',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKE045: {
        type: 'string',
        title: '疾病类型',
        enum: this.supDic.getSFDic('AKE045'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKE042: {
        type: 'string',
        title: '疾病编码',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      JBMC: {
        type: 'string',
        title: '疾病名称',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKA042: {
        type: 'string',
        title: '入院类型',
        readOnly: true,
        enum: this.supDic.getSFDic('AKA042'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC041: {
        type: 'string',
        title: '入院日期',
        readOnly: true,
        ui: {
          widget: 'date',
          grid: { span: 6 },
        },
      },
      AKC043: {
        type: 'string',
        title: '出院日期',
        readOnly: true,
        ui: {
          widget: 'date',
          grid: { span: 6 },
        },
      },
      AKC044: {
        type: 'string',
        title: '出院原因',
        readOnly: true,
        enum: this.supDic.getSFDic('AKC044'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      CKC907: {
        type: 'string',
        title: '天数',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      DAZ040: {
        type: 'string',
        title: '报销事由',
        readOnly: true,
        enum: this.supDic.getSFDic('DAZ040'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      DAZ041: {
        type: 'string',
        title: '发票张数',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      DAZ042: {
        type: 'string',
        title: '就诊次数',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC078: {
        type: 'string',
        title: '主要病情诊断',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
    },
    ui: {
      spanLabelFixed: 130,
      grid: { span: 7 },
    },
  };

  columns: STColumn[] = [
    {
      title: '药品疗诊类别',
      index: 'AKC054',
      dic: 'AKC054',
    },
    {
      title: '中心项目编码',
      index: 'AKE015',
    },
    {
      title: '中心项目名称',
      index: 'AKE036',
    },
    {
      title: '项目等级',
      index: 'AKA008',
      dic: 'AKA008',
    },
    {
      title: '单价',
      index: 'AKC055',
    },
    {
      title: '数量',
      index: 'AKC056',
    },
    {
      title: '金额',
      index: 'AKC057',
    },
    {
      title: '自理金额',
      index: 'AKC058',
    },
    {
      title: '自费金额',
      index: 'AKC059',
    },
    {
      title: '历年账户自费费用',
      index: 'AKC328',
    },
    {
      title: '单复方标志',
      index: 'BKA001',
      dic: 'BKA001',
    },
    {
      title: '归并类别',
      index: 'AKA005',
      dic: 'AKA005',
    },
    {
      title: '医院收费项目名称',
      index: 'AKC052',
    },
    {
      title: '用药时期',
      index: 'BKE020',
    },
    {
      title: '操作区',
      buttons: [
        {
          text: '删除',
          click: (record: any) => this.delete(record),
          pop: true,
          popTitle: '确定删除该任务？',
        },
      ],
    },
  ];

  fhSchema: SFSchema = {
    properties: {
      AKC046: {
        type: 'string',
        title: '医疗费总额',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC258: {
        type: 'string',
        title: '自理费用',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC259: {
        type: 'string',
        title: '自费费用',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC277: {
        type: 'string',
        title: '自负费用',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC327: {
        type: 'string',
        title: '限额以上可报医疗费',
        readOnly: true,
        ui: {
          grid: { span: 6 },
        },
      },
      AKC260: {
        type: 'string',
        title: '起付标准金额',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC274: {
        type: 'string',
        title: '个人帐户支付金额',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC275: {
        type: 'string',
        title: '本年帐户支付金额',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC261: {
        type: 'string',
        title: '基本医疗统筹支付',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC262: {
        type: 'string',
        title: '大病救助统筹支付',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC263: {
        type: 'string',
        title: '公务员补助统筹支付',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC264: {
        type: 'string',
        title: '记帐金额',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKC276: {
        type: 'string',
        title: '现金支付',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
    },
    ui: {
      spanLabelFixed: 130,
      grid: { span: 7 },
    },
  };

  newSchema: SFSchema = {
    properties: {
      AKC054: {
        type: 'string',
        title: '药品诊疗类别',
        enum: this.supDic.getSFDic('AKC054'),
        ui: {
          onlyVisual: true,
          widget: 'select',
          allowClear: true,
          grid: { span: 12 },
        },
      },
      AKE015: {
        type: 'string',
        title: '中心项目编码',
        ui: {
          onlyVisual: true,
          widget: 'custom',
          grid: { span: 12 },
        },
      },
      AKE036: {
        type: 'string',
        title: '中心项目名称',
        readOnly: true,
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      AKA008: {
        type: 'string',
        title: '项目等级',
        enum: this.supDic.getSFDic('AKA008'),
        readOnly: true,
        ui: {
          onlyVisual: true,
          widget: 'select',
          allowClear: true,
          grid: { span: 12 },
        },
      },
      AKC055: {
        type: 'string',
        title: '单价',
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      BAC014: {
        type: 'string',
        title: '中草药贴数',
        ui: { hidden: true, grid: { span: 6 } },
      },
      AKB040: {
        type: 'string',
        title: '自付比例',
        ui: { hidden: true, grid: { span: 6 } },
      },
      AKC056: {
        type: 'string',
        title: '数量',
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      AKC057: {
        type: 'string',
        title: '金额',
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      AKC058: {
        type: 'string',
        title: '自理金额',
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      AKC059: {
        type: 'string',
        title: '自费金额',
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      AKC328: {
        type: 'string',
        title: '历年账户自费费用',
        ui: {
          onlyVisual: true,
          grid: { span: 12 },
        },
      },
      BKA001: {
        type: 'string',
        title: '单复方标志',
        enum: this.supDic.getSFDic('BKA001'),
        readOnly: true,
        ui: {
          onlyVisual: true,
          widget: 'select',
          allowClear: true,
          grid: { span: 12 },
        },
      },
      AKA005: {
        type: 'string',
        title: '归并类别',
        enum: this.supDic.getSFDic('AKA005'),
        readOnly: true,
        ui: {
          onlyVisual: true,
          widget: 'select',
          allowClear: true,
          grid: { span: 12 },
        },
      },
      AKC052: {
        type: 'string',
        title: '医院收费项目名称',
        ui: {
          grid: { span: 12 },
        },
      },
      BKE020: {
        type: 'string',
        title: '用药时期',
        enum: this.supDic.getSFDic('BKE020'),
        ui: {
          onlyVisual: true,
          widget: 'select',
          allowClear: true,
          grid: { span: 12 },
        },
      },
    },
    ui: {
      spanLabelFixed: 200,
      grid: { span: 6 },
    },
    required: [
      'AKC054',
      'AKE015',
      'AKE036',
      'AKA008',
      'AKC055',
      'AKC056',
      'AKC057',
      'AKC058',
      'AKC059',
      'AKC328',
      'BKA001',
      'AKA005',
      'BKE020',
    ],
  };

  searchSchema: SFSchema = {
    properties: {
      AKC054: {
        type: 'string',
        title: '药品诊疗类别',
        enum: this.supDic.getSFDic('AKC054'),
        ui: {
          widget: 'select',
          width: 250,
          allowClear: true,
        },
      },
      AKE015: {
        type: 'string',
        title: '中心项目编码',
        ui: {
          width: 250,
        },
      },
      AKE036: {
        type: 'string',
        title: '中心项目名称',
        ui: {
          width: 250,
        },
      },
    },
  };

  ypColumns: STColumn[] = [
    { title: '项目编码', index: 'AKE015', dic: 'AKE015' },
    { title: '项目名称', index: 'AKE016' },
    { title: '复方药标志', index: 'AKE020', dic: 'AKE020' },
    { title: '单位', index: 'AKE024' },
    { title: '规格', index: 'AKE025' },
    { title: '剂型', index: 'AKE026' },
    { title: '项目等级', index: 'AKA008', dic: 'AKA008' },
    { title: '发票归并', index: 'AKA005', dic: 'AKA005' },
    {
      title: '操作区',
      buttons: [
        {
          text: '添加',
          click: record => this.tjpz(record),
        },
      ],
    },
  ];

  // st分页相关
  stpage = {
    total: true,
    show: true, // 显示分页
    front: false, // 关闭前端分页，true是前端分页，false后端控制分页
  };
  stchange: STChange = {
    type: 'pi',
    pi: 1,
    ps: 10,
    total: 0,
  };

  tabMode = 'bjxx';
  @ViewChild('pop', { static: false }) pop: SFComponent;
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @ViewChild('sf1', { static: false }) sf1: SFComponent;
  @ViewChild('sf2', { static: false }) sf2: SFComponent;
  @ViewChild('slxx', { static: false }) slxx: SFComponent;
  @ViewChild('wjfw', { static: false }) wjfw: LbWjfwComponent;
  @ViewChild('changePop', { static: false }) changePop: SFComponent;
  @ViewChild('mxst', { static: false }) mxst: GridComponent;
  @ViewChild('pzst', { static: false }) pzst: GridComponent;

  clsmcolumns: STColumn[] = [
    { title: '序号', index: 'RN', width: '10%' },
    { title: '材料名称', index: 'STUFFNAME', width: '50%' },
    { title: '材料数量', index: 'STUFFNUM', width: '25%' },
    { title: '收件类型', index: 'STUFFTYPE', width: '15%' },
  ];
  page: STPage = {
    show: false,
  };

  ngOnInit() {
    if (this.activateroute.snapshot.queryParams.PROJID) {
      this.PROJID = this.activateroute.snapshot.queryParams.PROJID;
      this.DAD069 = this.activateroute.snapshot.queryParams.DAD069;
      this.YLBX_QUERY();
      this.YBCL_QUERY();
      this.params = { sname: 'YLBXCL_QUERY', form: { PROJID: this.PROJID } };
    }
  }
  //打开情况下再次传入值
  _onReuseInit(): void {
    if (this.activateroute.snapshot.queryParams.PROJID) {
      this.PROJID = this.activateroute.snapshot.queryParams.PROJID;
      this.DAD069 = this.activateroute.snapshot.queryParams.DAD069;
      this.YLBX_QUERY();
      this.YBCL_QUERY();
      this.params = { sname: 'YLBXCL_QUERY', form: { PROJID: this.PROJID } };
    }
  }
  //事项进度
  selectTab(str: string) {
    this.tabMode = str;
    if (str === 'sxjd') {
    }
  }
  //界面赋值
  YLBX_QUERY() {
    this.lbservice
      .lbservice('YLBX_QUERY', { para: { PROJID: this.PROJID } })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.sf.formData = resdata.message.list[0];
          this.sf.reset();
        }
      });
  }
  //材料列表
  YBCL_QUERY() {
    this.lbservice
      .lbservice('YBCL_QUERY', {
        para: { PROJID: this.PROJID },
      })
      .then(resdata => {
        if (resdata.code === 0) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.dataset = resdata.message.list;
        }
      });
  }
  //中心项目编码查询
  yp_query() {
    this.lbservice
      .lbservice('YP_QUERY', {
        para: { sf2: this.sf2.value },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error('未查到该药品！');
        } else {
          this.date2 = resdata.message.list;
        }
      });
  }
  //受理保存
  lc_insert() {
    this.lbservice
      .lbservice('bxlr_insert', {
        para: {
          PROJID: this.PROJID,
          DAD069: this.DAD069,
          sf: this.sf.value,
          sf1: this.sf1.value,
          slxx: this.slxx.value,
        },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          if (this.slxx.getValue('/DAD095') === '0') {
            this.msgSrv.info('操作成功');
          } else {
            this.msgSrv.info('保存成功');
          }
          this.sf.formData = null;
          this.sf.reset();
        }
      });
  }
  //待遇计算
  dyjs_insert() {
    this.lbservice
      .lbservice('ylyjs_insert', {
        para: {
          PROJID: this.PROJID,
          sf: this.sf.value,
          sf1: this.sf1.value,
          slxx: this.slxx.value,
        },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.msgSrv.info('计算完成，请核对金额');
          this.formData1 = resdata.message;
        }
      });
  }
  add() {
    this.isVisible = true;
  }
  ypAdd() {
    this.pop.setValue('/AKE015', null);
    this.sf2.refreshSchema();
    this.pzst.reload({});
    this.isPzVisible = true;
  }
  tjpz(rec) {
    this.pop.setValue('/AKE015', rec.AKE015);
    this.pop.setValue('/AKE036', rec.AKE016);
    this.pop.setValue('/BKA001', rec.AKE020);
    //this.pop.setValue('/AKE024', rec.AKE024);
    //this.pop.setValue('/AKE025', rec.AKE025);
    //this.pop.setValue('/AKE026', rec.AKE026);
    this.pop.setValue('/AKA008', rec.AKA008);
    this.pop.setValue('/AKA005', rec.AKA005);
    this.isPzVisible = false;
  }
  handleCancel() {
    this.isVisible = false;
  }
  handlePzCancel() {
    this.isPzVisible = false;
  }
  handleOk(value: any) {
    this.lbservice
      .lbservice('YLBX_MXADD', {
        para: { l1: this.pop.value, sf: this.sf.value },
      })
      .then(resdata => {
        if (resdata.code > 0) {
          this.msgSrv.success('新建成功');
        } else {
          this.msgSrv.error(resdata.errmsg);
        }
        this.isVisible = false;
        this.query();
      });
  }
  stReload() {
    this.query();
  }

  query(pi1 = false) {
    // this.stchange.pi = 1;
    // this.stReload();
    if (pi1) {
      this.mxst.pi = 1;
    }
    this.mxst.reload({ PROJID: this.PROJID });
  }
  delete(rec: any) {
    this.lbservice
      .lbservice('YPMX_DELETE', { para: { CKC017: rec.CKC017 } })
      .then(resdata => {
        if (resdata.code > 0) {
          this.msgSrv.success('删除成功');
        } else {
          this.msgSrv.error(resdata.errmsg);
        }
        this.query(true);
      });
  }
  // tslint:disable-next-line:all
  pname = '受理登记信息';
  // tslint:disable-next-line: member-ordering
  active = true;
  // tslint:disable-next-line: member-ordering
  pname1 = '明细列表';
  // tslint:disable-next-line: member-ordering
  active1 = true;
  // tslint:disable-next-line: member-ordering
  pname2 = '计算结果';
  // tslint:disable-next-line: member-ordering
  active2 = true;
  // tslint:disable-next-line: member-ordering
  icon1 = 'caret-right';
}
